package com.sumapay.fsb.gensrc.controller;


import com.alibaba.fastjson.JSON;
import com.sumapay.fsb.common.util.CommonUtil;
import com.sumapay.fsb.gensrc.service.IFsbSmsRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suma
 * @since 2021-02-26
 */
@RestController
@RequestMapping("/sms")
public class FsbSmsRequestController {

    @RequestMapping("/getDynamicCode")
    public String getDynamicCode(ServletRequest servletRequest) {

        Map result = new HashMap<String,Object>();

        // 在内存中创建图象
        int width=85, height=20;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics g = image.getGraphics();
        // 生成随机类
        Random random = new Random();
        // 设定背景色
        g.setColor(CommonUtil.getRandColor(200,250));
        g.fillRect(0, 0, width, height);
        // 设定字体
        g.setFont(new Font("Times New Roman",Font.PLAIN,18));
        // 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到
        g.setColor(CommonUtil.getRandColor(160,200));
        for (int i=0;i<155;i++)
        {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x,y,x+xl,y+yl);
        }
        // 取随机产生的认证码(6位字符串)
        String sRand="";
        for (int i=0;i<6;i++){
            String rand=String.valueOf(CommonUtil.ELEMENTS[random.nextInt(58)]);
            sRand+=rand;
            // 将认证码显示到图象中
            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
            // 调用函数出来的颜色相同，可能是因为种子太接近，所以只能直接生成
            g.drawString(rand,13*i+6,16);
        }
        //赋值验证码
        result.put("dynamicCode",sRand);

        //图象生效
        g.dispose();
        ByteArrayInputStream inputStream=null;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        StringBuilder sb = new StringBuilder();
        try{
            ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
            ImageIO.write(image, "JPEG", imageOut);
            imageOut.close();
            inputStream = new ByteArrayInputStream(output.toByteArray());

            InputStreamReader input = new InputStreamReader(inputStream);
            BufferedReader bf = new BufferedReader(input);
            String line = null;
            while((line = bf.readLine()) != null){
                sb.append(line);
            }
        }catch(Exception e){

        }
        //result.put("image",sb.toString());
        result.put("imageUrl","http://stage.sumapay.com/fsb/yzm/1598252694258.png");

        return JSON.toJSONString(result);
    }

    @RequestMapping("/getVerificationCode")
    public String getVerificationCode(ServletRequest servletRequest) {
        MultiValueMap<String, Object> requestMap = CommonUtil.processRequestParameter(servletRequest);
        String result = iFsbSmsRequestService.sendSms(requestMap);
        System.out.println(result);
        return result;
    }

    private IFsbSmsRequestService iFsbSmsRequestService;

    @Autowired
    public void setiFsbSmsRequestService(IFsbSmsRequestService iFsbSmsRequestService){
        this.iFsbSmsRequestService = iFsbSmsRequestService;
    }
}
